package com.tmxk.jdbc04.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class BaseDao {
    // 定义了 driver url username password 静态实例
    protected static String driver;
    protected static String url;
    protected static String username;
    protected static String password;
    // Connection 数据库连接对象
    protected Connection conn;
    // PreparedStatement 预编译传输实例对象
    protected PreparedStatement pstmt;
    // ResultSet 查询结果集实例对象
    protected ResultSet rs;

    // 1. 建立数据库连接
    public Connection getConn(){
        // 获取driver url username password 这四个参数对应值
        driver = ConfigManager.getInstance().getProperty("oracle.driver");
        url = ConfigManager.getInstance().getProperty("oracle.url");
        username = ConfigManager.getInstance().getProperty("oracle.username");
        password = ConfigManager.getInstance().getProperty("oracle.password");
        try {
            //① 加载数据库驱动
            Class.forName(driver);
            //② 建立数据库连接对象
            conn = DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }

    //2.关闭数据库所有连接
    public void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn){
        try {
            if(rs != null){
                rs.close();
            }
            if(pstmt != null){
                pstmt.close();
            }
            if(conn != null){
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    //3.定义公共的查询方法
    public ResultSet executeQuery(String sql , Object...params){
        try {
            //建立数据库连接
            conn = getConn();
            // 建立数据库连接通道,传输预编译sql语句
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                //设置预编译占位符参数
                pstmt.setObject(i+1, params[i]);
            }
            // 触发执行sql语句,返回rs结果集
            rs = pstmt.executeQuery();
        }catch (Exception e){
            e.printStackTrace();
        }
        return rs;
    }

    //4.定义公共的增删改方法
    public int executeUpdate(String sql, Object...params){
        //定义一个变量,用来接收受影响的行数
        int result = 0;
        try{
            //建立数据库连接
            conn = getConn();
            // 建立数据库连接通道,传输预编译sql语句
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                //设置预编译占位符参数
                pstmt.setObject(i+1,params[i]);
            }
            // 触发执行sql语句,返回受影响的行数
            result = pstmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //释放所有资源
            closeAll(rs,pstmt,conn);
        }
        return result;
    }


    public static void main(String[] args) {
        BaseDao bd = new BaseDao();
        System.out.println(bd.getConn());
    }
}
